package com.ppc.service.impl;

import com.ppc.exception.BaseException;
import com.ppc.mapper.admin.SystemMapper;
import com.ppc.pmatch.MSKObj;
import com.ppc.pmatch.PKObj;
import com.ppc.pmatch.SKObj;
import com.ppc.pmatch.SetupObj;
import com.ppc.service.SystemService;
import com.ppc.utils.PMatchUtil;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author dy
 * @date 2023/12/20
 */
@Slf4j
@Service
public class SystemServiceImp implements SystemService {


  @Autowired
  private SystemMapper systemMapper;

  /**
   * @despription 初始化系统
   */
  @Override
  public void initialSystem() {
    if (!PMatchUtil.getHasSetup()) {
      SetupObj setupObj = PMatchUtil.initiatSystem();
      PKObj PK = setupObj.getPK();
      List<SKObj> SKSET = setupObj.getSKSET();
      MSKObj MSK = setupObj.getMSK();
      //存好PKMSK(file形式)
      PMatchUtil.writePKToFile(PMatchUtil.PKObjtoPKFile(PK));
      PMatchUtil.writeMSKToFile(PMatchUtil.MSKObjtoMSKFile(MSK)); //
      //存SKPO到数据库
      for (SKObj SK : SKSET) {
        systemMapper.add(PMatchUtil.SKObjtoSKPO(SK));
        log.info("添加sk {}", SK);
      }
      PMatchUtil.setHasSetup(true);
    } else {
      throw new BaseException("已初始化");
    }
  }
}
